%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% unlthesis.cls
%%% UNL thesis document class
%%%
%%% By João Lourenço (joao.lourenco@di.fct.unl.pt)
%%% Version of July 2014
%%%
%%% BUGS and SUGGETSIONS: please send email to "joao.lourenco@fct.unl.pt"
%%%     or submit a ticket in the the project web page at
%%%     http://code.google.com/p/thesisdifctunl/
%%%
%%% CONTRIBUTERS are welcome. Please contact "joao.lourenco@fct.unl.pt"
%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{unlthesis}[2014/07/01]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PROCESS KEY-VAL OPTIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{kvoptions-patch}%%%NEW
\RequirePackage{xkeyval}%
\RequirePackage{xifthen}




% --------------------------------------------------------
% DOCUMENT TYPE		UNL@doctype=?
%
% BSc report			0
% MSc preliminary report	1
% MSc dissertation		2
% PhD proposal			3
% PhD preliminary report	4
% PhD thesis			5

\DeclareOptionX{doctype}{
	\typeout{'doctype'=#1}
	\ifthenelse{\equal{#1}{bsc}}{\def\UNL@doctype{0}}
	{\ifthenelse{\equal{#1}{prepmsc}}{\def\UNL@doctype{1}}
	{\ifthenelse{\equal{#1}{msc}}{\def\UNL@doctype{2}}{
	{\ifthenelse{\equal{#1}{propphd}}{\def\UNL@doctype{3}}{
	}{\ifthenelse{\equal{#1}{prepphd}}{\def\UNL@doctype{4}}{
	}{\ifthenelse{\equal{#1}{phd}}{\def\UNL@doctype{5}}{
	\PackageError{thesisdifctunl}{Wrong value for option 'doctype': #1}{Please see the documentation}
	\expandafter\endinput
	}}}}}}}
}


% --------------------------------------------------------
% MAIN LANGUAGE		UNL@lang=?
%
% Portuguese			0
% English			1

\DeclareOptionX{lang}{
	\typeout{'lang'=#1}
	\ifthenelse{\equal{#1}{pt}\OR\equal{#1}{PT}}{\def\UNL@lang{0}}
	{\ifthenelse{\equal{#1}{en}\OR\equal{#1}{EN}}{\def\UNL@lang{1}}{
	\PackageError{thesisdifctunl}{Wrong value for option 'lang'}{Please see the documentation}
	\expandafter\endinput
	}}
}


% --------------------------------------------------------
% ENCODING		Anything valid for package "inputenc"
%
% utf8
% latin1
% ...

\DeclareOptionX{enc}{
	\typeout{'enc'=#1}
	\PassOptionsToPackage{#1}{inputenc}
}


% --------------------------------------------------------
% BIBLATEX OPTIONS		Are passed to the biblatex package
%
% style= numeric | alphabetic | 
% ...
%
% \DeclareOptionX{style}{
% 	\typeout{'biblatex style'=#1}
% 	\PassOptionsToPackage{style=#1}{biblatex}
% }
% \DeclareOptionX{sorting}{
% 	\typeout{'biblatex sorting'=#1}
% 	\PassOptionsToPackage{sorting=#1}{biblatex}
% }
\DeclareOptionX{biblatex}{
 	\typeout{'biblatex'=#1}
	\PassOptionsToPackage{#1}{biblatex}
}


% --------------------------------------------------------
% BOOK OPTIONS		Are passed to the main class (book)
%
% twoside
% a4paer
% ...

\DeclareOptionX{book}{
	\typeout{'book'=#1}
	\PassOptionsToClass{#1}{book}
}


% --------------------------------------------------------
% DEFAULT OPTIONS
\PassOptionsToPackage{portuguese,english}{babel}

\ExecuteOptionsX{
	doctype=msc,
	lang=en,
	enc=utf8,
}



% --------------------------------------------------------
% BIBBYCHAPTER		Ignored for now
\let\@bibbychapter=0
% \DeclareOption{bibbychapter}{
% 	\let\@bibbychapter=1
% }



% --------------------------------------------------------
% PROCESSING OPTIONS
\ProcessOptionsX\relax



% --------------------------------------------------------
% LOAD MAIN CLASS AND ADDITIONAL PACKAGES
\LoadClass{book}
\RequirePackage{inputenc}
\RequirePackage[T1]{fontenc}
\RequirePackage{babel}
\RequirePackage{csquotes}
\RequirePackage[toc]{appendix}
\RequirePackage{titlesec}           % To format the headings
\RequirePackage{fancyhdr}           % Headings configuration
\RequirePackage{setspace}\onehalfspacing           % One-and-half spacing
\RequirePackage[scaled]{helvet}
\RequirePackage{courier}
\RequirePackage{palatino}
\RequirePackage{ifpdf}		% This is now the recommended way for checking for PDFLaTeX:
\ifpdf
	\RequirePackage[pdftex]{graphicx}
	\DeclareGraphicsExtensions{.pdf, .png, .jpg, .tif}
	\RequirePackage[pdftex,usenames]{color}
	\definecolor{mydarkblue}{rgb}{0.0,0.08,0.45}
	\RequirePackage[
		pdftex,breaklinks,colorlinks,pdfdisplaydoctitle,citecolor=mydarkblue,
		linkcolor=mydarkblue,urlcolor=mydarkblue
		]{hyperref}
\else
	\RequirePackage{graphicx}
	\DeclareGraphicsExtensions{.eps, .png, .jpg}
	\RequirePackage[usenames]{color}
	\definecolor{mydarkblue}{rgb}{0.0,0.08,0.45}
	\RequirePackage[
		breaklinks,colorlinks,pdfdisplaydoctitle,citecolor=mydarkblue,
		linkcolor=mydarkblue,urlcolor=mydarkblue
		]{hyperref}
\fi
\RequirePackage[top=3cm,bottom=3cm,inner=3.5cm,outer=2.5cm]{geometry}[2010/02/12]



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PACKAGES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





% AAdd an image to the backgroung
\RequirePackage{eso-pic}
\newcommand{\BackgroundPic}[1]{
\put(0,0){
\parbox[b][\paperheight]{\paperwidth}{%
\vfill
\centering
\includegraphics[width=\paperwidth,height=\paperheight,
keepaspectratio]{#1}%
\vfill
}}}


\RequirePackage{ctable}

\RequirePackage[%
	backend=bibtex,
% sorting=nyt,%
% style=alphabetic,%
% labelyear=false,%
	maxalphanames=2,
	defernumbers=true,%
	maxcitenames=2,%
	maxnames=20,%
	firstinits=true,% will render all first and middle names as initials
	uniquename=init,%
% dashed=false,%
	sortcites=true,
]{biblatex}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% GENERAL PURPOSE MACROS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \let\origcleardoublepage=\cleardoublepage
% \def\cleardoublepage{%
%   \newpage{\pagestyle{empty}\origcleardoublepage}
% }

%%
% For use with the pseudocode package
% \def\@lopcchapterspace{\relax}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LABELS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Some relevant strings

\gdef\@singlemadviserstr{Orientador}
\gdef\@singlefadviserstr{Orientadora}
\gdef\@manymadviserstr{Orientadores}
\gdef\@manyfadviserstr{Orientadoras}
\gdef\@singlemcoadviserstr{Co-orientador}
\gdef\@singlefcoadviserstr{Co-orientadora}
\gdef\@manymcoadviserstr{Co-orientadores}
\gdef\@manyfcoadviserstr{Co-orientadoras}

\gdef\@mbachelordegreestr{Licenciado}
\gdef\@fbachelordegreestr{Licenciada}
\gdef\@mmastersdegreestr{Mestre}
\gdef\@fmastersdegreestr{Mestre}
\gdef\@mphddegreestr{Doutor}
\gdef\@fphddegreestr{Doutora}

% \gdef\doutor{Dr.\ }
% \gdef\doutora{Drª.\ }
% \gdef\profdoutor{Prof.\ Dr.\ }
% \gdef\profdoutora{Profª.\ Drª.\ }

\gdef\@bachelordissertationstr{Trabalho de Licenciatura}
\gdef\@mastersdissertationstr{Dissertação de Mestrado}
\gdef\@mastersqualifyingstr{Preparação de Dissertação}
\gdef\@phdqualifyingstr{Preparação de Tese}
\gdef\@phdproposalstr{Proposta de Tese de Doutoramento}
\gdef\@phdthesisstr{Tese de Doutoramento}

\gdef\@jurystr{Júri}

\gdef\UNL@bscdissertationtstr{Relatório para obtenção do Grau 
de\UNL@degree em\\\@majorfield}
\gdef\@prepmscdissertationtstr{Relatório intermédio para obtenção do Grau 
de\UNL@degree em\\\@majorfield}
\gdef\UNL@mscdissertationtstr{Dissertação para obtenção do Grau 
de\UNL@degree em\\\@majorfield}
\gdef\@propphhdissertationtstr{Proposta de tese para obtenção do Grau 
de \UNL@degree em\\\@majorfield}
\gdef\@prepphhdissertationtstr{Relatório intermédio para obtenção do Grau de\UNL@degree em\\\@majorfield}
\gdef\@phhdissertationtstr{Dissertação para obtenção do Grau 
de\UNL@degree em\\\@majorfield}

\gdef\@presentationtext{\UNL@doctype}

\gdef\thecopyrightstr{%
\noindent
Copyright \copyright\ \@author, \@faculty, \@university

\bigskip
\noindent
A \@faculty\ e a \@university\ têm o direito, perpétuo e sem limites geográficos, de arquivar e publicar esta dissertação através de exemplares impressos reproduzidos em papel ou de forma digital, ou por qualquer outro meio conhecido ou que venha a ser inventado, e de a divulgar através de repositórios científicos e de admitir a sua cópia e distribuição com objectivos educacionais ou de investigação, não comerciais, desde que seja dado crédito ao autor e editor.}

%% Language dependent

\gdef\@abstrnamePT{Resumo}
\gdef\@abstrnameEN{Abstract}

\gdef\@keywordsnamePT{Palavras-chave}
\gdef\@keywordsnameEN{Keywords}


%% Language Dependent

% Portuguese
\if\UNL@lang0
  \gdef\@notdefined{NÃO DEFINIDO}
  \gdef\acknowledgementsname{Agradecimentos}
  \def\lstlistlistingname{Listagens}
% English
\else\if\UNL@lang1
  \gdef\@notdefined{UNDEFINED}
  \gdef\acknowledgementsname{Acknowledgements}
\fi\fi


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% IDENTIFICATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \def\universitylogo#1{%
%   \gdef\@universitylogo{#1}}
% \def\@universitylogo{~universitylogo \@notdefined}
% 
% \def\universitylogowidth#1{%
%   \gdef\@universitylogowidth{#1}}
% \def\@universitylogowidth{2cm}

% Default cover background
\def\thesiscoverphd#1{%
  \gdef\@thesiscoverphd{#1}}
\def\@thesiscoverphd{~thesiscoverphd \@notdefined}

\def\thesiscovermsc#1{%
  \gdef\@thesiscovermsc{#1}}
\def\@thesiscovermsc{~thesiscovermsc \@notdefined}

\def\thesiscoverplain#1{%
  \gdef\@thesiscoverplain{#1}}
\def\@thesiscoverplain{~thesiscoverempty \@notdefined}


% \def\university#1{%
%   \gdef\@university{#1}}
% \def\@university{~university \@notdefined}
% 
% \def\faculty#1{%
%   \gdef\@faculty{#1}}
% \def\@faculty{~faculty\@notdefined}
% 
% \def\department#1{%
%   \gdef\@department{#1}}
% \def\@department{~department \@notdefined}

\def\majorfield#1{%
  \gdef\@majorfield{#1}}
\def\@majorfield{~majorfield \@notdefined}

\def\address#1{%
  \gdef\@address{#1}}
\def\@address{~address \@notdefined}

\def\faculty#1{%
  \gdef\@faculty{#1}}
\def\@majorfield{~faculty \@notdefined}

\def\university#1{%
  \gdef\@university{#1}}
\def\@majorfield{~university \@notdefined}

% \@input@{repeat}

\@input@{defaults}

%% Date

\def\themonth#1{\gdef\@themonth{#1}}
\def\@themonth{~themonth \@notdefined}

\def\theyear#1{\gdef\@theyear{#1}}
\def\@theyear{~theyear \@notdefined}


%% Authors identification
\def\author{%
  \@ifnextchar [%
    {\@pauthor}%
    {\@pauthor[m]}}

\def\@pauthor[#1]#2{%
  \if#1m
  	\def\authorgender{m}
  \else\if#1f
	\def\authorgender{f}
  \else
  \typeout{^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
!  Please specify author gender using [m] or [f] as the first^^J%
!  argument of the "author" macro.  See the template! ;)^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
}
   \errhelp{Please be gentle and follow the template recommendations.}
   \errmessage{Please specify author gender!}
  \fi\fi

  \gdef\@author{#2}
  \hypersetup{pdfauthor={\@author}}}

\def\@author{~author \@notdefined}

\def\authordegree#1{%
  \gdef\@authordegree{#1}}
\def\@authordegree{~authordegree \@notdefined}


%% Advisors and Co-Advisers identification (at most 9 of each)
%% \adviser[m|f]{Name}{Category/Position}{School}
%% where N i a number from 1 to 9
\def\@theadvisergender{f}	% default for female advisers
\newcount\@theadvisercount	% number of advisers
\@theadvisercount 0\relax
\def\@thecoadvisergender{f}	% default for female co-advisers
\newcount\@thecoadvisercount	% number of co-advisers
\@thecoadvisercount 0\relax

% 
% \newcount\@myi
% \@myi 0\relax
% \loop
% 	\advance\@myi 1\relax
% 	\expandafter\let\csname @showadviser\number\@myi\endcsname=0
% 	\expandafter\let\csname @showcoadviser\number\@myi\endcsname=0
% \ifnum \@myi < 10
% \repeat

% \repeat \for{i} \from{1} \to{9} \do {
% 	\expandafter\let\csname @showadviser\number\i\endcsname=0
% 	\expandafter\let\csname @showcoadviser\number\i\endcsname=0
% 	\expandafter\let\csname @showjury\number\i\endcsname=0
% }


\def\adviser{%
  \@ifnextchar [%
    {\@padviser[]}%
    {\@padviser[][m]}}

\def\coadviser{%
  \@ifnextchar [%
    {\@padviser[co]}%
    {\@padviser[co][m]}}

% #1 = [] for adviser or [co] co co-adviser
% #2 = [m] or [f]
% #3 = {name}
% #4 = {category}
% #5 = {school/institution}
\def\@padviser[#1][#2]#3#4#5{%
  \expandafter\advance\csname @the#1advisercount\endcsname 1\relax
  \ifx#2m
  	\expandafter\def\csname @the#1advisergender\endcsname{m}
  \else\ifx#2f
  \else
  \typeout{^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
!  Please specify #1adviser gender using [m] or [f] as the second^^J%
!  argument of the "#1adviser" macro.  See the template! ;)^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
}
   \errhelp{Please be gentle and follow the template recommendations.}
   \errmessage{Please specify adviser gender!}
  \fi\fi
  \expandafter\gdef\csname @#1advisername\expandafter\number\csname @the#1advisercount\endcsname\endcsname{#3}
  \expandafter\gdef\csname @#1advisercat\expandafter\number\csname @the#1advisercount\endcsname\endcsname{#4}
  \expandafter\gdef\csname @#1adviserschool\expandafter\number\csname @the#1advisercount\endcsname\endcsname{#5}
}

\def\@theadviserstr[#1]{%
	\@ptheadviserstr[#1]{\expandafter\csname @the#1advisercount\endcsname}{\expandafter\csname @the#1advisergender\endcsname}%
}

\def\@ptheadviserstr[#1]#2#3{%
	\ifnum #2 > 1
  		\expandafter\csname @many#3#1adviserstr\endcsname
	\else
  		\expandafter\csname @single#3#1adviserstr\endcsname
	\fi
}

\def\printadviser[#1]#2{%
	\ifcsname @#1advisername#2\endcsname
		\ifnum #2 = 1
			& \\[-2ex]
			\@theadviserstr[#1]:
		\fi
		&
		\expandafter\csname @#1advisername#2\endcsname,\\&
		\expandafter\csname @#1advisercat#2\endcsname,
		\expandafter\csname @#1adviserschool#2\endcsname
	\\
	\fi
}




%% Work identification

\def\title#1{%
  \gdef\@title{#1}
  \hypersetup{pdftitle={\@title}}}
\def\@title{~title \@notdefined}

\def\@texttype{%
  \if\UNL@doctype0
    \@bachelordissertationstr
  \else\if\UNL@doctype1
    \@mastersqualifyingstr
  \else\if\UNL@doctype2
  \@mastersdissertationstr
  \else\if\UNL@doctype3
    \@phdproposalstr
  \else\if\UNL@doctype4
   \@phdqualifyingstr
   \else\if\UNL@doctype5
    \@phdthesisstr
  \fi\fi\fi\fi\fi\fi}

\def\thedegree#1#2{%
	\expandafter\csname @#1#2\endcsname}

\def\UNL@degree{%
  \if\UNL@doctype0
    \thedegree{\ader}{bachelordegreestr}
  \else\if\UNL@doctype1
    \thedegree{\authorgender}{mastersdegreestr}
  \else\if\UNL@doctype2
    \thedegree{\authorgender}{mastersdegreestr}
  \else\if\UNL@doctype3
    \thedegree{\authorgender}{phddegreestr}
  \else\if\UNL@doctype4
    \thedegree{\authorgender}{phddegreestr}
   \else\if\UNL@doctype5
    \thedegree{\authorgender}{phddegreestr}
  \fi\fi\fi\fi\fi\fi}



% Juri
\newcount\@thejurypcount	% number of presidents (must be one)
\@thejurypcount 0\relax
\newcount\@thejuryacount	% number of referees
\@thejuryacount 0\relax
\newcount\@thejuryvcount	% number of other memebrs of the jury
\@thejuryvcount 0\relax

\def\jury{%
  \@ifnextchar [%
    {\@pjury}%
    {\@pjury[v]}}


% #1 = [p] for president, [a] for referee, [v] for others
% #2 = {name}
\def\@pjury[#1]#2{%
	\expandafter\advance\csname @thejury#1count\endcsname 1\relax
  	\expandafter\gdef\csname @jury#1name\expandafter\number\csname @thejury#1count\endcsname\endcsname{#2}
}

\def\printthejuryp{%
	\ifnum \@thejurypcount = 1
		& \\[-2ex]
		Presidente: & \csname @jurypname1\endcsname \\
	\else
  		\typeout{^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
!  Please identify one and only one president of the jury with^^J%
!  the [p] argument of the "jury" macro.  See the template! ;)^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
}
   		\errhelp{Please be gentle and follow the template recommendations.}
   		\errmessage{Please specify president of the jury!}
	\fi
}

\def\@printthejurya[#1]{%
	\ifcsname @juryaname#1\endcsname
	\ifnum #1 = 1
		& \\[-2ex]
		Arguente\ifnum \@thejuryacount > 1 s\fi:
	\fi
	& \expandafter\csname @juryaname#1\endcsname\relax \\
	\fi
}

\def\printthejurya{%
	\ifnum \@thejuryacount > 0
		\@printthejurya[1]
		\@printthejurya[2]
		\@printthejurya[3]
		\@printthejurya[4]
		\@printthejurya[5]
		\@printthejurya[6]
		\@printthejurya[7]
		\@printthejurya[8]
		\@printthejurya[9]
	\else
  		\typeout{^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
!  Please identify at least one referee with the [a] argument ^^J%
!  of the "jury" macro.  See the template! ;)^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
}
   		\errhelp{Please be gentle and follow the template recommendations.}
   		\errmessage{Please specify referees of the jury!}
	\fi
}

\def\@printthejuryv[#1]{%
	\ifcsname @juryvname#1\endcsname
	\ifnum #1 = 1
		& \\[-2ex]
		Voga\ifnum \@thejuryvcount = 1 l\else is\fi:
	\fi
	& \expandafter\csname @juryvname#1\endcsname\relax \\
	\fi
}

\def\printthejuryv{%
	\ifnum \@thejuryvcount > 0
		\@printthejuryv[1]
		\@printthejuryv[2]
		\@printthejuryv[3]
		\@printthejuryv[4]
		\@printthejuryv[5]
		\@printthejuryv[6]
		\@printthejuryv[7]
		\@printthejuryv[8]
		\@printthejuryv[9]
	\else
  		\typeout{^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
!  Please identify at least one jury with the [v] argument ^^J%
!  of the "jury" macro.  See the template! ;)^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
}
   		\errhelp{Please be gentle and follow the template recommendations.}
   		\errmessage{Please specify at least one element of the jury!}
	\fi
}

\def\printthejury{%
	\fontsize{11}{12}
	\selectfont
	\ctable[
		cap=,
		caption=,
		label=,
		pos=h,
		maxwidth=1\linewidth,
		right,
	]{rX}{}{
	& \@jurystr: \\
 	\printthejuryp
	\printthejurya
	\printthejuryv
	}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PAGE LAYOUT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\synctex=1 % Uase synctex
\brokenpenalty=10000

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Fonts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\quotefont{\normalfont\normalsize}
\newcommand\quotefonti{\it\normalsize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Including chapters and similars
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\dedicatoryfile#1{%
  \gdef\@dedicatoryfile{#1}}
\def\@dedicatoryfile{\@empty}

\ifx\@dedicatoryfile\@empty
\def\printdedicatory{}
\else
\def\printdedicatory{
	\if\UNL@doctype0
		\@input@{Chapters/\@dedicatoryfile}\global\let\@dedicatoryfile\@empty
	\else\if\UNL@doctype1
	\else\if\UNL@doctype2
		\@input@{Chapters/\@dedicatoryfile}\global\let\@dedicatoryfile\@empty
	\else\if\UNL@doctype3
	\else\if\UNL@doctype4
	\else\if\UNL@doctype5
		\@input@{Chapters/\@dedicatoryfile}\global\let\@dedicatoryfile\@empty
	\fi\fi\fi\fi\fi\fi
}
\fi


\def\quotefile#1{%
  \gdef\@quotefile{#1}}
\def\@quotefile{\@empty}

\ifx\@quotefile\@empty
\def\printquote{}
\else
\def\printquote{
	\if\UNL@doctype0
		\@input@{Chapters/\@quotefile}\global\let\@quotefile\@empty
	\else\if\UNL@doctype1
	\else\if\UNL@doctype2
		\@input@{Chapters/\@quotefile}\global\let\@quotefile\@empty
	\else\if\UNL@doctype3
	\else\if\UNL@doctype4
	\else\if\UNL@doctype5
		\@input@{Chapters/\@quotefile}\global\let\@quotefile\@empty
	\fi\fi\fi\fi\fi\fi
}
\fi

\def\acknowledgementsfile#1{%
  \gdef\@acknowledgementsfile{#1}}
\def\@acknowledgementsfile{~acknowledgementsfile \@notdefined}

\def\printacknowledgements{
	\if\UNL@doctype0
		\@input@{Chapters/\@acknowledgementsfile}\global\let\@acknowledgementsfile\@empty
	\else\if\UNL@doctype1
	\else\if\UNL@doctype2
		\@input@{Chapters/\@acknowledgementsfile}\global\let\@acknowledgementsfile\@empty
	\else\if\UNL@doctype3
	\else\if\UNL@doctype4
	\else\if\UNL@doctype5
		\@input@{Chapters/\@acknowledgementsfile}\global\let\@acknowledgementsfile\@empty
	\fi\fi\fi\fi\fi\fi
}

\def\ptabstractfile#1{%
  \gdef\@ptabstractfile{#1}}
\def\@ptabstractfile{\@empty}
% \def\@ptabstractfile{~ptabstractfile \@notdefined}

\def\enabstractfile#1{%
  \gdef\@enabstractfile{#1}}
\def\@enabstractfile{\@empty}
% \def\@enabstractfile{~enabstractfile \@notdefined}

\if\UNL@lang0   % Sumário-PT + Abstract-EN para tese em Português
\def\@ptabstractfile{~ptabstractfile \@notdefined}
\else\if\UNL@lang1   % Abstract-EN + Sumário-PT for thesis in English
\def\@enabstractfile{~enabstractfile \@notdefined}
\fi\fi


\def\printabstract{%
	\if\UNL@lang0   % Sumário-PT + Abstract-EN para tese em Português
		\selectlanguage{portuguese}
		\@input@{Chapters/\@ptabstractfile}\global\let\@ptabstractfile\@empty
		\ifx\@enabstractfile\@empty
		\else
			\selectlanguage{english}
			\@input@{Chapters/\@enabstractfile}\global\let\@enabstractfile\@empty
			\selectlanguage{portuguese}
		\fi
	\else\if\UNL@lang1   % Abstract-EN + Sumário-PT for thesis in English
		\selectlanguage{english}
		\@input@{Chapters/\@enabstractfile}\global\let\@enabstractfile\@empty
		\ifx\@ptabstractfile\@empty
		\else
			\selectlanguage{portuguese}
			\@input@{Chapters/\@ptabstractfile}\global\let\@ptabstractfile\@empty
			\selectlanguage{english}
		\fi
	\fi\fi
}

\def\thesisfrontmatter{%
	\frontmatter
	\pagestyle{fancy}
	\pagenumbering{roman} 
	\fancyhead{}
	\setlength{\headheight}{15pt}
	\fancyhead[RO,LE]{}
	% \fancyhead[RO,LE]{\leftmark\hfill\rightmark}
	% \def\chaptermark##1{\markboth{\fontfamily{phv}%
	% 	\selectfont\footnotesize\thechapter.\ \textsc{##1}}{}}
	% \def\sectionmark##1{\markright{\fontfamily{phv}%
	%  	\selectfont\footnotesize\thesection.\ ##1}}
	\if\UNL@lang0   % Tese em Português
		\selectlanguage{portuguese}
	\else\if\UNL@lang1   % Thesis in English
		\selectlanguage{english}
	\fi\fi
}

\def\thesismainmatter{%
	\mainmatter
	\pagestyle{fancy}
	\pagenumbering{arabic} 
	\fancyhead{}
	\setlength{\headheight}{15pt}
	\fancyhead[RO,LE]{\leftmark\hfill\rightmark}
	\def\chaptermark##1{\markboth{\fontfamily{phv}%
		\selectfont\footnotesize\thechapter.\ \textsc{##1}}{}}
	\def\sectionmark##1{\markright{\fontfamily{phv}%
	 	\selectfont\footnotesize\thesection.\ ##1}}
	\if\UNL@lang0   % Tese em Português
		\selectlanguage{portuguese}
	\else\if\UNL@lang1   % Thesis in English
		\selectlanguage{english}
	\fi\fi
}

\def\endthesismainmatter{%
	\fancyhead{}
	\lhead{\nouppercase{\textsc{\leftmark}}}
	\rhead{}
}


% Chapters
\newcount\@chapcount
\@chapcount 0\relax

\def\chapterfile#1{%
	\advance\@chapcount 1\relax
	\expandafter\gdef\csname @chapterfile\number\@chapcount\endcsname{Chapters/#1}
}

\def\printchapthers{%
	\newcount\@myi
	\@myi 0\relax
	\loop
		\advance\@myi 1\relax
		\def\@chapname{\expandafter\csname @chapterfile\number\@myi\endcsname}
		\if\@bibbychapter1
			\begin{refsegment}
		\fi
		\@input@{\@chapname}
		\if\@bibbychapter1
			\printbibliography[segment=\arabic{chapter},heading=subbibintoc]
			\end{refsegment}
		\fi
	\ifnum \@myi < \@chapcount
	\repeat
	\endthesismainmatter
}


% Appendixes
\newcount\@appxcount
\@appxcount 0\relax

\def\appendixfile#1{%
	\advance\@appxcount 1\relax
	\expandafter\gdef\csname @appendixfile\number\@appxcount\endcsname{Chapters/#1}
}

\def\printappendixes{%	
	\appendix
	\newcount\@myi
	\@myi 0\relax
	\loop
		\advance\@myi 1\relax
		\def\@appxname{\expandafter\csname @appendixfile\number\@myi\endcsname}
		\@input@{\@appxname}
	\ifnum \@myi < \@appxcount
	\repeat
}


\def\printlistoffigures{%
  \gdef\@printlistoffigures{\listoffigures}}
\def\@printlistoffigures{}

\def\printlistoftables{%
  \gdef\@printlistoftables{\listoftables}}
\def\@printlistoftables{}

\def\printlistoflistings{%
  \gdef\@printlistoflistings{\lstlistoflistings}}
\def\@printlistoflistings{}

\def\printotherlists{%
	\@printlistoffigures
	\@printlistoftables
	\if\UNL@lang0   % Estamos a usar o Português
		\def\lstlistlistingname{Listagens}
	\fi
	\@printlistoflistings
}


\def\addbibfile#1{%
  \addbibresource{#1}}

\def\printbib{%
	\if\@bibbychapter0
		\printbibliography
	\fi
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Frontpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\frontpage{
\thispagestyle{empty}

\if\UNL@doctype0
	\def\@defcoverbg{\@thesiscovermsc}
	\newgeometry{tmargin=4cm,bmargin=1.3cm,lmargin=4.5cm,rmargin=3.3cm}
\else\if\UNL@doctype1
	\def\@defcoverbg{\@thesiscovermsc}
	\newgeometry{tmargin=4cm,bmargin=1.3cm,lmargin=4.5cm,rmargin=3.3cm}
\else\if\UNL@doctype2
	\def\@defcoverbg{\@thesiscovermsc}
	\newgeometry{tmargin=4cm,bmargin=1.3cm,lmargin=4.5cm,rmargin=3.3cm}
\else\if\UNL@doctype3
	\def\@defcoverbg{\@thesiscoverphd}
	\newgeometry{tmargin=4cm,bmargin=1.3cm,lmargin=5.5cm,rmargin=2.5cm}
\else\if\UNL@doctype4
	\def\@defcoverbg{\@thesiscoverphd}
	\newgeometry{tmargin=4cm,bmargin=1.3cm,lmargin=5.5cm,rmargin=2.5cm}
\else\if\UNL@doctype5
	\def\@defcoverbg{\@thesiscoverphd}
	\newgeometry{tmargin=4cm,bmargin=1.3cm,lmargin=5.5cm,rmargin=2.5cm}
\else
	\def\@defcoverbg{\@thesiscoverplain}
	\newgeometry{tmargin=4cm,bmargin=1.3cm,lmargin=4.5cm,rmargin=3.3cm}
\fi\fi\fi\fi\fi\fi
	
\AddToShipoutPicture*{\BackgroundPic{\@defcoverbg}}

\begin{center}
	\fontfamily{phv}

% Name of Candidate
	\fontsize{15}{17}
	\selectfont
	\textbf{\@author}
	
\vspace{0.25cm}	
% Academic qualifications
	\fontsize{12}{13}
	\selectfont
	\@authordegree

\vspace{4.25cm}
% Title of Dissertation
	\fontsize{18}{20}
	\selectfont
	\textbf{\@title}\\[0.75cm]

\vspace{0.75cm}
% Degree
	\fontsize{12}{13}
	\selectfont
	\@presentationtext
	
	\vfill
	% Advisers
	\fontsize{13}{14}
	\selectfont
	\ctable[
		cap=,
		caption=,
		label=,
		pos=h,
		maxwidth=1\linewidth,
	]{rX}{}{
		\printadviser[]{1}
		\printadviser[]{2}
		\printadviser[]{3}
		\printadviser[]{4}
		\printadviser[]{5}
		\printadviser[]{6}
		\printadviser[]{7}
		\printadviser[]{8}
		\printadviser[]{9}
		\printadviser[co]{1}
		\printadviser[co]{2}
		\printadviser[co]{3}
		\printadviser[co]{4}
		\printadviser[co]{5}
		\printadviser[co]{6}
		\printadviser[co]{7}
		\printadviser[co]{8}
		\printadviser[co]{9}
}


\vfill\vfill
% Referee board
\if\UNL@doctype0
\printthejury
\else\if\UNL@doctype1
\else\if\UNL@doctype2
\printthejury
\else\if\UNL@doctype3
\printthejury
\else\if\UNL@doctype4
\else\if\UNL@doctype5
\printthejury
\fi\fi\fi\fi\fi\fi

\vfill\vfill\vfill

% Date
\fontsize{12}{13}
\selectfont
\textbf{\@themonth, \@theyear}
	
\end{center}
\restoregeometry
\newpage
\thispagestyle{empty}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copyright page
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\printcopyrightpage{%
	\cleardoublepage
	\ 
	\vfill
	\noindent
	\textbf{\large \@title}

	\bigskip
	\thecopyrightstr
}

\def\printcopyright{
\if\UNL@doctype0
	\printcopyrightpage
\else\if\UNL@doctype1
\else\if\UNL@doctype2
	\printcopyrightpage
\else\if\UNL@doctype3
\else\if\UNL@doctype4
\else\if\UNL@doctype5
	\printcopyrightpage
\fi\fi\fi\fi\fi\fi}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Dedicatory
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\dedicatory{%
	\if@openright\cleardoublepage\else\clearpage\fi
	\thispagestyle{empty}
	~\\[2cm]
    \begin{flushright}
    \begin{minipage}{100mm}
    \quotefonti
    \begin{flushright}}
\def\enddedicatory{
	\end{flushright}
    \normalfont
    \end{minipage}
	\end{flushright}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Quote
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\quote{%
	\if@openright\cleardoublepage\else\clearpage\fi
	\thispagestyle{empty}
	~\\[2cm]
    \begin{flushright}
    \begin{minipage}{100mm}
    \quotefonti
    \begin{flushright}}
\def\endquote{
	\end{flushright}
    \normalfont
    \end{minipage}
	\end{flushright}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Acknowledgements
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\acknowledgements{%
  \chapter*{\acknowledgementsname}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Resumo
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\abstractPT{%
  \selectlanguage{portuguese}
  \gdef\@keywordsname{\@keywordsnamePT}
  \chapter*{\@abstrnamePT}
   \rule{\textwidth}{0.2mm} ~\\[-3.5ex]
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Abstract
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\abstractEN{%
  \selectlanguage{english}
  \gdef\@keywordsname{\@keywordsnameEN}
  \chapter*{\@abstrnameEN}
  \rule{\textwidth}{0.2mm} ~\\[-3.5ex]
}
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Keywords
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\@keywordsname{\@defaultkeywordsname}
\def\keywords{%
  \par\vskip\baselineskip\noindent{\bf\@keywordsname: }}
\def\endkeywords{~\\[2ex]\rule{\textwidth}{0.2mm}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Quotations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Table of contents
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{tocdepth}{2}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Sectioning
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setcounter{secnumdepth}{3}

\definecolor{backGrayColor}{rgb}{0.7,0.7,0.7} % Background title color



\titleformat{\chapter}[display]
{\bfseries\Huge}				% format
{\hfill\colorbox{backGrayColor}{\fontsize{100}{120}\selectfont{\textcolor{white}{\thechapter}}}} % label
{1ex}							% sep
{%\titlerule
  \vspace{2ex}\filleft}			% before
%\colorbox{backGrayColor}{}
[\vspace{1cm}					%\titlerule
]


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Headers & footers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Bibliography
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Tables and figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Mathematics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Reference macros
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

